﻿//Copyright (c) 2008 Michael J. Ryan & Apollo Group, Inc.

//Permission is hereby granted, free of charge, to any person
//obtaining a copy of this software and associated documentation
//files (the "Software"), to deal in the Software without
//restriction, including without limitation the rights to use,
//copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the
//Software is furnished to do so, subject to the following
//conditions:

//The above copyright notice and this permission notice shall be
//included in all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.IO;
using Apollo.Common.Configuration;
using NLog;
using NLog.Config;

namespace Apollo.Common.Logging
{
	/// <summary>
	/// Simple static class to get an NLog logger.  Will use the "*nlog.config" via the Apollo.Common Configuration library.
	/// </summary>
	public class LogTool
	{
		private static readonly LogFactory _factory;
		private static readonly Logger _logger;
		
		static LogTool()
		{
			ConfigurationFile cf = Config.GetFile("nlog");
			if (cf != null) {
				FileInfo cfg = cf.FileInfo;
				if (cfg != null && cfg.Exists) {
					_factory = new LogFactory(
						new XmlLoggingConfiguration(
							cfg.FullName, 
							true
						)
					);
				}
			}
			
			_logger = GetLogger("Apollo.Common.Logging");
		}
		
		public static Logger GetLogger()
		{
			return _logger;
		}
		
		public static Logger GetLogger(string name)
		{
			if (_factory != null)
				return _factory.GetLogger(name);
			else
				return LogManager.GetLogger(name);
		}
		
		public static Logger GetLogger(Type type)
		{
			return GetLogger(type.FullName);
		}
		
		public static Logger Logger
		{
			get { return GetLogger(); }
		}
		
#region Logging Methods (static)
	#region Trace
		public static void Trace(string message)
		{
			_logger.Trace(message);
		}
		
		public static void TraceException(string message, Exception exception)
		{
			_logger.TraceException(message, exception);
		}	
	#endregion
		
	#region Debug
		public static void Debug(string message)
		{
			_logger.Debug(message);
		}
		
		public static void DebugException(string message, Exception exception)
		{
			_logger.DebugException(message, exception);
		}
	#endregion
		
	#region Info
		public static void Info(string message)
		{
			_logger.Info(message);
		}
		
		public static void InfoException(string message, Exception exception)
		{
			_logger.InfoException(message, exception);
		}	
	#endregion
		
	#region Warn
		public static void Warn(string message)
		{
			_logger.Warn(message);
		}
		
		public static void WarnException(string message, Exception exception)
		{
			_logger.WarnException(message, exception);
		}	
	#endregion
		
	#region Error
		public static void Error(string message)
		{
			_logger.Error(message);
		}
		
		public static void ErrorException(string message, Exception exception)
		{
			_logger.ErrorException(message, exception);
		}	
	#endregion
		
	#region Fatal
		public static void Fatal(string message)
		{
			_logger.Fatal(message);
		}
		
		public static void FatalException(string message, Exception exception)
		{
			_logger.FatalException(message, exception);
		}	
	#endregion
#endregion

	}
}
